package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 查询购物车
     */
    @Override
    public List<ShoppingCart> queryShoppingCart() {
        return shoppingCartMapper.queryShoppingCart(BaseContext.getCurrentId());
    }

    /**
     * 添加购物车
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId()); // 用户编号
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);

        // 购物车已存在
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list != null && !list.isEmpty()) {
            ShoppingCart s = list.get(0);
            s.setNumber(s.getNumber() + 1);
            shoppingCartMapper.update(s);
            return;
        }

        // 第一次添加购物车 菜品或套餐
        if (shoppingCartDTO.getDishId() != null) {
            // 菜品
            Dish dish = dishMapper.getById(shoppingCart.getDishId());
            shoppingCart.setName(dish.getName());
            shoppingCart.setAmount(dish.getPrice()); // 菜品的金额
            shoppingCart.setImage(dish.getImage());
        } else {
            // 套餐
            SetmealVO setmeal = setmealMapper.select(shoppingCart.getSetmealId().intValue());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setAmount(setmeal.getPrice()); // 菜品的金额
            shoppingCart.setImage(setmeal.getImage());
        }
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCartMapper.insert(shoppingCart);
    }

    /**
     * 删除购物车
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 购物车已存在
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.queryShoppingCart(BaseContext.getCurrentId());
        for (ShoppingCart shoppingCart : shoppingCarts) {
            if (shoppingCart.getDishId() != null && shoppingCart.getDishId().equals(shoppingCartDTO.getDishId()) ||
                    shoppingCart.getSetmealId() != null && shoppingCart.getSetmealId().equals(shoppingCartDTO.getSetmealId())) {
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                if (shoppingCart.getNumber() == 0) {
                    shoppingCartMapper.delShoppingCart(shoppingCart.getId());
                    return;
                }
                shoppingCartMapper.update(shoppingCart);
                return;
            }
        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        shoppingCartMapper.delByUserId(BaseContext.getCurrentId());
    }
}
